// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 22/03/2011 12:18:08 p.m.
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   KernellUtils.java

package com.luxsoft.sw3.impap.ui;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import com.luxsoft.siipap.model.User;
import com.luxsoft.siipap.security.AutorizacionWindow;
import com.luxsoft.siipap.service.KernellSecurity;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.swing.controls.SXAbstractDialog;
import com.luxsoft.siipap.swing.utils.MessageUtils;
import com.luxsoft.siipap.swing.utils.SWExtUIManager;
import java.awt.BorderLayout;
import java.io.PrintStream;
import javax.swing.*;

public class KernellUtils
{
    protected static class PasswordDialog extends SXAbstractDialog
    {

        protected JComponent buildContent()
        {
            JPanel panel = new JPanel(new BorderLayout());
            FormLayout layout = new FormLayout("p,2dlu,100dlu", "");
            DefaultFormBuilder builder = new DefaultFormBuilder(layout);
            builder.setDefaultDialogBorder();
            userField = new JTextField(20);
            passwordField = new JPasswordField(20);
            passwordField.addActionListener(getOKAction());
            builder.append("Usuario", userField);
            builder.append("Password", passwordField);
            panel.add(builder.getPanel(), "Center");
            getCancelAction().putValue("Name", "Cancelar");
            panel.add(buildButtonBarWithOKCancel(), "South");
            return panel;
        }

        public String getPaswword()
        {
            return new String(passwordField.getPassword());
        }

        public String getUserName()
        {
            return userField.getText();
        }

        private JTextField userField;
        private JPasswordField passwordField;

        public PasswordDialog()
        {
            super("Autentificando usuario");
        }
    }


    public KernellUtils()
    {
    }

    public static User buscarUsuarioPorPassword(String password)
    {
        return KernellSecurity.instance().findUser(password, ServiceLocator2.getHibernateTemplate());
    }

    public static User buscarUsuario()
    {
        AutorizacionWindow window = new AutorizacionWindow("Autorizacion en l\355nea");
        window.setConComentario(false);
        window.setManager(ServiceLocator2.getAutorizacionesManager());
        window.open();
        if(!window.hasBeenCanceled())
        {
            User user = window.getUser().getUser();
            System.out.println(user);
            return user;
        } else
        {
            return null;
        }
    }

    public static boolean validarAcceso(String role)
    {
        User user = buscarUsuario();
        if(user != null)
        {
            if(user.hasRole(role))
                return true;
            MessageUtils.showMessage((new StringBuilder("No tiene el Rol: ")).append(role).toString(), "Acceso denegado");
        }
        return false;
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                SWExtUIManager.setup();
                KernellUtils.buscarUsuario();
                System.exit(0);
            }

        }
);
    }
}
